#include "WavEncoder.h"

WavData* getDataFromWav(sbytep fileName)
{
	FILE* fileWav;
	WavHeader header;
	WavData* wav;

	errno_t err = fopen_s(&fileWav, fileName, "rb");

	if(err)
	{
		printf("%s doesn't exist! Error: %d", fileName, err);
		return 0;
	}
	
	fread_s(&header, sizeof(WavHeader), sizeof(WavHeader), 1, fileWav);

	wav = (WavData*) malloc(sizeof(WavHeader) + header.subchunk2Size);
	
	wav->header = header;
	wav->data.data = (bytep) malloc(header.subchunk2Size);
	wav->data.size = header.subchunk2Size;

	printf("%x\n", *(int*)header.chunkId);

	fread(wav->data.data, wav->header.subchunk2Size, 1, fileWav);

	fclose(fileWav);

	return wav;
}

void putDataOnWav(WavData* wavData)
{
	int i = 0;
	FILE* fileWav;
	
	errno_t err = fopen_s(&fileWav, "C:\\Users\\Darky\\Desktop\\IPU\\C-Project\\Debug\\output2.wav", "w+");
	
	if(err)
	{
		printf("Error while open: %d", err);
		return;
	}

	fwrite(&(wavData->header), sizeof(WavHeader), 1, fileWav);
	fwrite(wavData->data.data, wavData->header.subchunk2Size, 1, fileWav);

	fclose(fileWav);
}
